import 'user_comment.dart';
import 'user_participated_activity.dart';
import 'activity_data.dart';

/// 用户内容响应模型（对应MyApplication2的MinePublishContentBean）
/// 直接对应API文档中的响应格式，与MyApplication2保持一致
class UserContentResponse {
  final String status;
  final String? message;
  final List<ActivityData> activities;
  final List<ActivityData> endedActivities;
  final List<UserComment> comments;
  final List<UserParticipatedActivity> participatedActivities;

  UserContentResponse({
    required this.status,
    this.message,
    required this.activities,
    required this.endedActivities,
    required this.comments,
    required this.participatedActivities,
  });

  bool get isSuccess => status == 'success';

  factory UserContentResponse.fromJson(Map<String, dynamic> json) {
    return UserContentResponse(
      status: json['status'] ?? 'error',
      message: json['message'], // 可选字段
      activities: (json['activities'] as List? ?? [])
          .map((e) => ActivityData.fromJson(e))
          .toList(),
      endedActivities: (json['ended_activities'] as List? ?? [])
          .map((e) => ActivityData.fromJson(e))
          .toList(),
      comments: (json['comments'] as List? ?? [])
          .map((e) => UserComment.fromJson(e))
          .toList(),
      participatedActivities: (json['participated_activities'] as List? ?? [])
          .map((e) => UserParticipatedActivity.fromJson(e))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      if (message != null) 'message': message,
      'activities': activities.map((e) => e.toJson()).toList(),
      'ended_activities': endedActivities.map((e) => e.toJson()).toList(),
      'comments': comments.map((e) => e.toJson()).toList(),
      'participated_activities': participatedActivities.map((e) => e.toJson()).toList(),
    };
  }

  /// 便捷方法：获取所有发布的活动（进行中的 + 已结束的）
  List<ActivityData> get allActivities => [...activities, ...endedActivities];

  /// 便捷方法：检查是否有内容
  bool get hasContent => 
      activities.isNotEmpty || 
      endedActivities.isNotEmpty || 
      comments.isNotEmpty || 
      participatedActivities.isNotEmpty;

  /// 便捷方法：获取统计信息
  Map<String, int> get contentStats => {
    'activities': activities.length,
    'endedActivities': endedActivities.length,
    'comments': comments.length,
    'participatedActivities': participatedActivities.length,
    'totalActivities': allActivities.length,
  };
}

